import Cookies from 'js-cookie'

const TokenKey = 'token'

export function getToken() {
  return sessionStorage.getItem(TokenKey) || void 0
}

export function setToken(token) {
  return Cookies.set(TokenKey, token,{ expires: 3 })
}

export function removeToken() {
  return Cookies.remove(TokenKey)
}

export function clearCookies() {
  var keys = document.cookie.match(/[^ =;]+(?=\=)/g)
  if (keys) {
    for (var i = keys.length; i--;) {
      // 清除当前域名下的,例如：m.kevis.com
      document.cookie = keys[i] + '=0;path=/;expires=' + new Date(0).toUTCString()
      // 清除当前域名下的，例如 .m.kevis.com
      document.cookie = keys[i] + '=0;path=/;domain=' + document.domain + ';expires=' + new Date(0).toUTCString()
      // 清除一级域名下的或指定的，例如 .kevis.com
      document.cookie = keys[i] + '=0;path=/;domain=kevis.com;expires=' + new Date(0).toUTCString()
    }
  }
}
